<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 条码管理
 * @author zxj
 *
 */
class Mgbarcode extends MY_Controller
{
    //private $wsdl_url = 'http://sun.erp.com:8889/webService/GetNewSkuForBackPieces.php';
    private $wsdl_url = 'http://stock.dhhtgroup.com/webService/GetNewSkuForBackPieces.php';

    function __construct()
    {
        parent::__construct();
        $this->load->model('order_model', 'orderModel');
        $this->load->model('product_model');
        $this->load->model('brand_model');
        $this->load->library('barcode');
    }



    function addBarcode($oid = ''){
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $formData = gp();
        $oid = empty($oid)?$formData['oid']:trim($oid);
        $data['oid'] = $oid;
        $orderFirst = $this->common_model->selectFirst('maintain_order', array('maintain_id' => $oid));

        /**
         * 部门兼容两套数据
         */

        if($orderFirst['website_origin']){
            $emailFirst['name'] = $orderFirst['website_origin'];
        }else{
            //获取邮箱对应名称
            $emailFirst = $this->common_model->selectFirst('mt_customers', array('email' => $orderFirst['consumer_email']));
            if(!$orderFirst['consumer_email'] || !$emailFirst){
                show_goto('orders/order/edit/'.$oid,lang('barcode_yxwtts'));
            }
        }

        //选择入库
        if ($formData['submits']) {
            $pids = $formData['pid'];
            $is_repairs = $formData['is_repair'];
            $damage_names = $formData['damage_name'];
            $stor_ids = $formData['stor_id'];
            $skus = $formData['sku'];
            for($i=0;$i<count($pids);$i++){
                //基础数据
                $productFirst = $this->product_model->get_product($pids[$i]);
                $qcReportFirst = $this->product_model->getQcReport($pids[$i]);
                $attachments = $qcReportFirst['attachment'].';'.$productFirst['other_fujian_name'];
                //获取仓库信息
                $storFirst = $this->common_model->selectFirst('mt_stor_ck',array('id'=>$stor_ids[$i]));
                $to_post_data = array();
                $to_post_data['token'] = 'maintain';
                $to_post_data['account'] = 'maintain_sys';
                $to_post_data['sku'] = $skus[$i];
                $to_post_data['level'] = $damage_names[$i];
                $to_post_data['stockId'] = $storFirst['ck_identifier'];
                $to_post_data['orderSource'] = $emailFirst['name'];
                if($orderFirst['website_origin']=='6'){
                    $to_post_data['orderSource'] = 'smtwb';
                }
                $to_post_data['oid'] = $oid;
                $to_post_data['pid'] = $pids[$i];
                $to_post_data['wgzkson'] = $productFirst['wgzkson'];
                $to_post_data['attachments'] = $attachments;
                $soapSkuFirst = $this->vpost($this->wsdl_url,$to_post_data);
                $soapSkuFirst = json_decode($soapSkuFirst,true);
                log_output($to_post_data);
                log_output($soapSkuFirst);
                if($soapSkuFirst['success']!=1){
                    if(MYDEBUG=='true' && $this->users['account']=='zlj'){
                        gsh_output($soapSkuFirst,1);
                    }
                    gsh_output('error:'.$soapSkuFirst['message'],1);
                }
                $htmlBarcode = $this->barcode->getBarcodeHTML($soapSkuFirst['new_sku'], "C128");
                $indata = array(
                    'barcode' => $htmlBarcode, 'sku' => $soapSkuFirst['new_sku'],
                    'title' => $soapSkuFirst['name'], 'pid' => $pids[$i],
                    'oid' => $oid, 'damage_name' => $damage_names[$i],
                    'is_repair'=>$is_repairs[$i],'old_sku'=>$skus[$i],
                    'stor_identifier'=>$storFirst['ck_identifier'],'stor_id'=>$stor_ids[$i],
                    'stor_name'=>$storFirst['ck_name'],'branch'=>$emailFirst['name'],
                    'wgzkson'=>$productFirst['wgzkson'],'attachments'=>$attachments,
                    'add_user'=>$this->users['account'],'add_time'=>time()
                );
                $barcodeFirst = $this->common_model->selectFirst('print_barcode_list',array('pid'=>$pids[$i]));
                if(!$barcodeFirst){
                    $this->common_model->insert('print_barcode_list',$indata);
                }else{
                    $this->common_model->update('print_barcode_list',$indata,array('pid'=>$pids[$i]));
                }

            }
            //修改订单状态
            $orderData = array('status_id'=>21);
            $this->editExtend($oid,$orderData);
            show_goto('ajax/ajax/printBarcode/'.$oid,lang('barcode_cjdydlcg'));
        }else{
            //查询当前订单的产品
            $productList = $this->product_model->get_product('', $oid);
            //获取产品额外数据
            foreach ($productList as $key=>&$rs) {
                $rs['pid'] = $rs['product_id'];
                //判断是否同意维修
                $is_accept = $this->common_model->selectFirst('maintain_product_price',array('pid'=>$rs['pid']));
                if($is_accept && !isset($is_accept['is_accept_price'])){
                    show_goto('orders/order/inDetails/'.$oid,'请先选择是否接受报价');
                }
                $rs['is_repair'] = $is_accept?1:0;
                //外观状况
                if($is_accept && $is_accept['is_accept_price']!=1){
                    $rs['damage_id'] = 6;
                }else if($rs['brand_id']==73){
                    $rs['damage_id'] = 1;
                }
                $damagedFirst = $this->common_model->selectFirst('maintain_damage', array('damage_id' => $rs['damage_id']));
                $rs['damage_name'] = $damagedFirst['damage_name'];
                $rs['damage_name_lang'] = MYLANG=='sc'?$damagedFirst['damage_name']:$damagedFirst['damage_name_en'];
            }
            unset($rs);
            $data['productList'] = $productList;
            //获取仓库
            $storckList = $this->common_model->selectCustom('mt_stor_ck');
            $data['storckList'] = $storckList;

            //视图以及样式
            $this->breadcrumb->append_crumb(array('url'=>'', 'name'=>''));
            $this->load->view('defaults/mgbarcode_post',$data);
            $this->load->view('defaults/common/footer');
        }

    }



    function vpost($url, $data)
    {   // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $tmpInfo; // 返回数据
    }



    /**
     * 修改订单和维修轨迹
     * @param string $id
     * @param array $orderData
     * @return bool
     */
    function editExtend($id = '',$orderData=array()){
        if(!$id || !$orderData)return false;
        $this->orderModel->addOrSetOrder($orderData,$id);
        //维修轨迹
        $trackData = array(
            'maintain_id'=>$id,'add_time'=>date('Y-m-d H:i:s'),
            'target_status'=>$orderData['status_id'],'modified_user'=>$this->users['account']
        );
        $this->orderModel->set_order_track($id,$trackData);
    }



}
